package com.dubbo.lian.cms.modules.app.service.impl;
 import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dubbo.lian.common.util.BaseR;
import com.dubbo.lian.cms.modules.app.UserCoin;
import com.dubbo.lian.cms.modules.app.mapper.UserCoinMapper;
import com.dubbo.lian.cms.modules.app.service.IUserCoinService;
import com.dubbo.lian.common.util.ReturnTemplateUtil;
import org.springframework.stereotype.Service;
 import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 /**
 *
 * UserCoin 表数据服务层接口实现类
 *
 */
@Service
public class UserCoinServiceImpl extends ServiceImpl<UserCoinMapper, UserCoin> implements IUserCoinService {
     @Override
    public BaseR selectUserCoinRank(JSONObject json) {
        String userid = json.getString("userid");
        EntityWrapper selectCoinRankParam = new EntityWrapper();
        List<UserCoin> coinList = super.selectList(selectCoinRankParam);
         List<UserCoin> coinListSort = coinList.stream()
                .sorted((e1, e2) -> e2.getHongmengCoin().compareTo(e1.getHongmengCoin()))
                .collect(Collectors.toList());
         int rank= 1;
        flag:
        for (UserCoin userCoin : coinListSort) {
            if(userid.equals(userCoin.getUserid())){
                break flag;
            }
            rank++;
        }
        Map rankMap = new HashMap<>();
        rankMap.put("coinRank",rank);
        return ReturnTemplateUtil.returnOfMap(rankMap);
    }
     @Override
    public BaseR selectUserCoinSum(JSONObject json) {
        String userid = json.getString("userid");
        UserCoin userCoin = selectById(userid);
        if(userCoin==null){
            UserCoin userCoinNull = new UserCoin();
            userCoinNull.setUserid(userid);
            userCoinNull.setHongmengCoin(0);
            return ReturnTemplateUtil.returnOfObject(userCoinNull);
        }else{
            return ReturnTemplateUtil.returnOfObject(userCoin);
        }
    }
 }